package robot

import (
	// "fmt"

	"regexp"

	"github.com/jinzhu/configor"
	"github.com/kataras/iris"
)

var Config = struct {
	Robot struct {
		Ua []string
	}
}{}

func Configurator(app *iris.Application) {
	app.Use(func(ctx iris.Context) {
		configor.Load(&Config, "conf/config.yml")
		// fmt.Printf("ctx: %#v", Config)
		userAgent := ctx.GetHeader("User-Agent")
		length := len(Config.Robot.Ua)
		match := false
		for i := 0; i < length; i++ {
			s := Config.Robot.Ua[i]
			regx := regexp.MustCompile(s)
			match = regx.MatchString(userAgent)
			if match {
				break
			}
		}
		if match {
			ctx.StatusCode(iris.StatusForbidden)
		} else {
			ctx.Next()
		}
	})
}
